package com.household.controller.api;


import com.household.domain.ResponseResult;
import com.household.domain.entity.User;
import com.household.domain.enums.ResponseErrorCodeEnum;
import com.household.domain.enums.SexEnum;
import com.household.service.UserService;
import com.household.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.annotation.AuthenticationPrincipal;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * API 设置信息
 */
@RestController
@RequestMapping("/api/v1/setting")
public class SettingApiController {
    @Autowired
    private UserService sysUserService;

    /**
     * 修改密码
     */
    @PostMapping("/password")
    public ResponseResult resetPassword(String origin, String target, @AuthenticationPrincipal User sysUser) {
        if(StringUtils.isAnyBlank(origin, target)) {
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }

        return sysUserService.resetPassword(sysUser.getId(), origin, target);
    }

    /**
     * 设置/修改手机号（暂未实现手机验证码code）
     */
    @PostMapping("/tel")
    public ResponseResult setTel(String tel, String code, HttpSession session, @AuthenticationPrincipal User sysUser) {
        if(StringUtils.isAnyBlank(tel, code)) {
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }

        return sysUserService.setTel(sysUser, tel, code, session);
    }

    /**
     * 设置/修改性别
     */
    @PostMapping("/sex")
    public ResponseResult setSex(Integer sex, @AuthenticationPrincipal User sysUser) {
        SexEnum sexEnum = SexEnum.getByType(sex);
        if(sexEnum == null) {
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }

        return sysUserService.setSex(sysUser, sexEnum);
    }


}
